package com.woniu.darryringmall.front.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniu.darryringmall.front.entity.*;
import com.woniu.darryringmall.front.service.MessageService;
import com.woniu.darryringmall.front.service.OrderService;
import com.woniu.darryringmall.front.service.ViewOrderService;
import com.woniu.darryringmall.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController("fontOrder")
@RequestMapping(value = "/front/order" , method = RequestMethod.GET)
public class OrderController {

    /**
     * 进入购物车并能正确展示订单数据
     *
     */
    @Autowired
    private ViewOrderService viewOrderService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private OrderService orderService;

    @Autowired
    private MessageService messageService;

    /**
     * 增加订单
     * @return
     */
    @PostMapping("add")
    public ResponseData add(@RequestBody Order order,HttpSession session){
        Order userOrder=new Order();
        userOrder.setOrderNo(MallUtils.getOrderNo());
        userOrder.setUserId(((User)session.getAttribute("user")).getId());
        userOrder.setAccept(order.getAccept());
        userOrder.setTelephone(order.getTelephone());
        userOrder.setAddress(order.getAddress());
        userOrder.setOrderTime(order.getOrderTime());
        userOrder.setPayType(order.getPayType());
        userOrder.setStatus("1");
        userOrder.setDeliveType(order.getDeliveType());
        userOrder.setEarnest(order.getEarnest());
        List<OrderItems> userOrderItems=order.getOrderItems();
        BigDecimal total=new BigDecimal(0);
        for(OrderItems o:userOrderItems){
            BigDecimal number=new BigDecimal(o.getNums());
            total=total.add(o.getPrice().multiply(number));
        }
        total.setScale(2);
        userOrder.setMoney(total);

        return ResponseUtils.SUCCESS;
    }

    /**
     * 修改订单状态(包括1 已提交，2 已支付，3 已取消，4 待发货，5 待收货，6 已收货，7 已完成 )
     *
     * @return
     */
    @PostMapping("updateStatus")
    public ResponseData updateStatus(int id,String status){
        UpdateWrapper updateWrapper=new UpdateWrapper();
        updateWrapper.eq("id",id);
        if("2".equals(status)){
            updateWrapper.set("status","2");
            updateWrapper.set("payTime",MallUtils.getNow());

        }else if ("3".equals(status)){
            updateWrapper.set("status","3");
        }else if("4".equals(status)){
            updateWrapper.set("status","4");
        }else if ("5".equals(status)){
            updateWrapper.set("status","5");
            updateWrapper.set("deliveTime",MallUtils.getNow());
        }else if ("6".equals(status)){
            updateWrapper.set("status","6");
            updateWrapper.set("receiveTime",MallUtils.getNow());
        }else {
            updateWrapper.set("status","7");
        }
        orderService.update(updateWrapper);
        return ResponseUtils.SUCCESS;
    }


    //根据userid查询订单列表
    @PostMapping("list1")
    public ResponseData orderlist (HttpSession session){
        session.setAttribute("vordersss",viewOrderService.getByUserId(2));
        System.out.println(viewOrderService.getByUserId(2));
        return new ResponseData(session.getAttribute("vordersss"));
    }

    //查询状态
    @PostMapping("statuslist")
    public ResponseData getByStatus (HttpSession session){
        int arr[]  = new int[3];
        //查询已取消的订单
        arr[0] = orderService.getByStatu3(((User)session.getAttribute("user")).getId()).size();
        //查询待发货
        arr[1] = orderService.getByStatu4(((User)session.getAttribute("user")).getId()).size();
        //查询待收货
        arr[2] = orderService.getByStatu5(((User)session.getAttribute("user")).getId()).size();
        return new ResponseData(arr);
    }

    //收货后发送系统信息
    @PostMapping("receiving")
    public ResponseData receiving(int id,HttpSession session,String orderNo){
        orderService.update(id,"7");
        User user = (User) session.getAttribute("user");
        Message message = new Message();
        message.setUserId(user.getId());
        message.setMessage(SystemMessageTempleat.receiving(orderNo));
        message.setStatus("未读");
        message.setOrderNo(orderNo);
        message.setLabel(SystemMessageTempleat.receiving);
        boolean flag = messageService.save(message);
        if(flag){
            redisTemplate.opsForValue().increment(user.getName());
        }
        return new ResponseData(ResultStateCode.SUCCESS,"已确认收货");
    }

    //计算总金额
    @PostMapping("getmoney")
    public ResponseData getMoney (HttpSession session) {
        return new ResponseData(orderService.getMoney(2));
    }

    /*@PostMapping("placeorder")
    public ResponseData placeorder () {

        return new ResponseData();
    }*/
}
